#include "HUD.h"
#include "ImgMgr.h"
#include "RessourceMgr.h"


HUD::HUD()
{
}

void HUD::init(ImgMgr *imageManager)
{
	m_font.loadFromFile("ressources/font.ttf");

	m_textWood.setFont(m_font);
	m_textRock.setFont(m_font);
	m_textFood.setFont(m_font);
	m_textPop.setFont(m_font);

	m_textWood.setCharacterSize(26);
	m_textRock.setCharacterSize(26);
	m_textFood.setCharacterSize(26);
	m_textPop.setCharacterSize(26);
	
	m_wood = imageManager->getSprite(5);
	m_rock = imageManager->getSprite(6);
	m_food = imageManager->getSprite(7);
	m_pop = imageManager->getSprite(89);

	m_wood.setScale(2, 2);
	m_rock.setScale(2, 2);
	m_food.setScale(2, 2);
	m_pop.setScale(2, 2);
}

void HUD::maj(sf::View *view, RessourceMgr *ressourceMgr)
{
	float posViewX = view->getCenter().x - view->getSize().x / 2;
	float posViewY = view->getCenter().y - view->getSize().y / 2;
	std::ostringstream oss;

	oss << ressourceMgr->getWood();
	m_textWood.setString(oss.str());
	oss.str("");

	oss << ressourceMgr->getRock();
	m_textRock.setString(oss.str());
	oss.str("");

	oss << ressourceMgr->getFood();
	m_textFood.setString(oss.str());
	oss.str("");

	oss << ressourceMgr->getPop();
	oss << "/";
	oss << ressourceMgr->getPopMax();
	m_textPop.setString(oss.str());

	m_wood.setPosition(sf::Vector2f(posViewX, posViewY));
	m_textWood.setPosition(m_wood.getPosition().x + 8, m_wood.getPosition().y + m_wood.getGlobalBounds().height - m_textWood.getGlobalBounds().height / 2); 
	m_rock.setPosition(sf::Vector2f(posViewX + m_wood.getGlobalBounds().width * 2, posViewY));
	m_textRock.setPosition(m_rock.getPosition().x + 8, m_rock.getPosition().y + m_rock.getGlobalBounds().height - m_textRock.getGlobalBounds().height / 2); 
	m_food.setPosition(sf::Vector2f(m_rock.getPosition().x + m_rock.getGlobalBounds().width * 2, posViewY));
	m_textFood.setPosition(m_food.getPosition().x + 8, m_food.getPosition().y + m_food.getGlobalBounds().height - m_textFood.getGlobalBounds().height / 2); 
	m_pop.setPosition(m_food.getPosition().x + m_food.getGlobalBounds().width * 2, posViewY);
	m_textPop.setPosition(m_pop.getPosition().x, m_pop.getPosition().y + m_pop.getGlobalBounds().height - m_textPop.getGlobalBounds().height / 2); 
	
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
	{
		m_wood.move(0, -4);
		m_rock.move(0, -4);
		m_food.move(0, -4);
		m_pop.move(0, -4);
		m_textWood.move(0, -4);
		m_textRock.move(0, -4);
		m_textFood.move(0, -4);
		m_textPop.move(0, -4);
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
	{
		m_wood.move(0, 4);
		m_rock.move(0, 4);
		m_food.move(0, 4);
		m_pop.move(0, 4);
		m_textWood.move(0, 4);
		m_textRock.move(0, 4);
		m_textFood.move(0, 4);
		m_textPop.move(0, 4);
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
	{
		m_wood.move(-4, 0);
		m_rock.move(-4, 0);
		m_food.move(-4, 0);
		m_pop.move(-4, 0);
		m_textWood.move(-4, 0);
		m_textRock.move(-4, 0);
		m_textFood.move(-4, 0);
		m_textPop.move(-4, 0);
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
	{
		m_wood.move(4, 0);
		m_rock.move(4, 0);
		m_food.move(4, 0);
		m_pop.move(4, 0);
		m_textWood.move(4, 0);
		m_textRock.move(4, 0);
		m_textFood.move(4, 0);
		m_textPop.move(4, 0);
	}
}

void HUD::render(sf::RenderWindow *app)
{
	app->draw(m_wood);
	app->draw(m_textWood);
	app->draw(m_rock);
	app->draw(m_textRock);
	app->draw(m_food);
	app->draw(m_textFood);
	app->draw(m_pop);
	app->draw(m_textPop);
}